<template>
	<form-container title="Api接口配置" :width="700" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="接口地址" name="requestUrl">
				<a-input v-model:value="formData.requestUrl" placeholder="请输入接口地址,例：sys/user/list" />
			</a-form-item>
			<a-form-item label="接口类型" name="requestType">
				<a-radio-group v-model:value="formData.requestType" :options="requestTypeOptions"> </a-radio-group>
			</a-form-item>
			<a-form-item label="显示标签" name="requestLabel">
				<a-input v-model:value="formData.requestLabel" placeholder="请输入表名" />
			</a-form-item>
			<a-form-item label="接口值" name="requestValue">
				<a-input v-model:value="formData.requestValue" placeholder="请输入表描述" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</form-container>
</template>

<script setup name="apiForm">
	import { required } from '@/utils/formRules'
	// 抽屉状态
	const visible = ref(false)
	// 定义emit事件
	const emit = defineEmits({ successful: null })
	//表单
	const formRef = ref()
	// 表单数据，也就是默认给一些数据
	const formData = ref({})
	//提交等待
	const submitLoading = ref(false)
	const apiInfo = ref()

	// 类型
	const requestTypeOptions = [
		{
			label: 'GET',
			value: 'get'
		},
		{
			label: 'POST',
			value: 'post'
		}
	]

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true //显示抽屉
		//表单数据
		formData.value = { requestType: 'GET', requestValue: 'id', requestLabel: 'name' }
		apiInfo.value = record
	}

	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		requestUrl: [required('请输入接口地址')],
		requestType: [required('请输入接口类型')],
		requestLabel: [required('请输入显示标签')],
		requestValue: [required('请输入接口值')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			apiInfo.value.requestUrl = formData.value.requestUrl
			apiInfo.value.requestType = formData.value.requestType
			apiInfo.value.requestLabel = formData.value.requestLabel
			apiInfo.value.requestValue = formData.value.requestValue
			submitLoading.value = false
			onClose()
		})
	}

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>

<style lang="scss" scoped></style>
